package ch07_GUI编程;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ImageIconDemo {
	public static void main(String[] args) throws IOException {
		BaseFrame f = new BaseFrame("ImgeIcon Demo");

		String dir = "images/";
		int count = 4;
		JLabel[] labs = new JLabel[count];
		ImageIcon[] imgs = new ImageIcon[count];
		imgs[0] = new ImageIcon(dir + "clock.png");
//目前无法通过URL(Uniform Resource Locator) 创建ImageIcon类；错误未知
//	ClassLoader loader = ImageIconDemo.class.getClassLoader();
//		URL u1 = loader.getResource("images/java.png");
//		imgs[1] = new ImageIcon(u1);
		imgs[1] = new ImageIcon(dir + "java.png");

//		 URL u2 =loader.getResource("images/apple.png");
//		 imgs[2]=new ImageIcon(Toolkit.getDefaultToolkit().createImage(u2));
		imgs[2] = new ImageIcon(dir + "apple.png");

		File file = new File(dir + "android.png");
		FileInputStream in = new FileInputStream(file);
		int fileLength = (int) file.length();
		byte[] bytes = new byte[fileLength];
		in.read(bytes, 0, fileLength);
		imgs[3] = new ImageIcon(bytes);
		in.close();

		f.setLayout(new GridLayout(2, 2, 5, 5));
		for (int i = 0; i < count; i++) {
			labs[i] = new JLabel(imgs[i]);
			labs[i].setOpaque(true);
			labs[i].setBackground(Color.WHITE);
			f.add(labs[i]);
		}

		String s = "宽：" + imgs[2].getIconWidth() + "高：" + imgs[2].getIconHeight();
		labs[2].setText(s);
		labs[2].setHorizontalAlignment(SwingConstants.CENTER);
		labs[2].setVerticalTextPosition(SwingConstants.BOTTOM);

		f.setIconImage(new ImageIcon(dir + "pc.png").getImage());
		f.showMe();
	}
}
